﻿<UserControl
			xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
			xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
			xmlns:behaviours="clr-namespace:LayoutPlanning.Behaviours"
			xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
			xmlns:shapes="clr-namespace:Toolbox.Shapes;assembly=Toolbox"
			xmlns:model="clr-namespace:LayoutPlanning.Model"
			xmlns:interactivity="clr-namespace:Mvvm.UI.Interactivity;assembly=Mvvm"
			xmlns:eventToCommand="clr-namespace:Mvvm.UI.Behaviors.EventToCommand;assembly=Mvvm"
			xmlns:interactivity1="clr-namespace:Mvvm.UI.Interactivity;assembly=Mvvm"
			xmlns:layoutPlanning="clr-namespace:LayoutPlanning"
			x:Name="userControl"
			x:Class="LayoutPlanning.Views.LayoutView"
			mc:Ignorable="d" d:DataContext="{Binding Main, Source={StaticResource Locator}}">




	<Border  BorderThickness="0.5" BorderBrush="IndianRed">
		<Grid ClipToBounds="True" DataContext="{Binding Main, Source={StaticResource Locator}}"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
			<interactivity1:Interaction.Behaviors>
				<eventToCommand:EventToCommand EventName="MouseWheel" Command="{Binding ZoomMouseWheel}" PassEventArgsToCommand="True" ></eventToCommand:EventToCommand>
			</interactivity1:Interaction.Behaviors>
			<Border  BorderThickness="0.5" BorderBrush="IndianRed">
				<ZoomableCanvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Scale="{Binding Zoom, Mode=OneWay}" Offset="{Binding Offset, Mode=OneWay, Converter={StaticResource PointToPos}}">
					<interactivity:Interaction.Behaviors>
						<behaviours:DragBehavior></behaviours:DragBehavior>
                        <eventToCommand:EventToCommand EventName="Loaded" Command="{Binding Main.NormalizeZoom, Source={StaticResource Locator}}"  CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ZoomableCanvas}}}" ></eventToCommand:EventToCommand>

                    </interactivity:Interaction.Behaviors>

					<Grid Background="AliceBlue"  Width="{Binding Layout.Width}" Height="{Binding Layout.Height}" >

						<ContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  DataContext="{Binding Layout.Layout}" Content="{Binding Element}">
							<interactivity:Interaction.Behaviors>
								<eventToCommand:EventToCommand EventName="DataContextChanged" Command="{Binding Main.NormalizeZoom, Source={StaticResource Locator}}"  CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ZoomableCanvas}}}" ></eventToCommand:EventToCommand>
                                
                            </interactivity:Interaction.Behaviors>

						</ContentControl>


						<ItemsControl   Visibility="{Binding ShowSound, Converter={StaticResource BoolToVisability}}" ItemsSource="{Binding RelationsWithSoundSensitivness}">
							<ItemsControl.ItemsPanel>
								<ItemsPanelTemplate>
									<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >

									</Grid>
								</ItemsPanelTemplate>
							</ItemsControl.ItemsPanel>
							<ItemsControl.ItemTemplate>
								<DataTemplate >
									<shapes:Arrow   Stroke="{Binding Station2.SoundColor}" HeadHeight="0" HeadWidth="0"  StrokeDashArray="2"   StrokeThickness="{Binding Station2.Loudness, Converter={StaticResource EnumToValue}}"  
										X1="{Binding Station1.CenterPoint.X, Mode=OneWay}" Y1="{Binding Station1.CenterPoint.Y, Mode=OneWay}" 
										X2="{Binding Station2.CenterPoint.X, Mode=OneWay}" Y2="{Binding Station2.CenterPoint.Y, Mode=OneWay}" />
								</DataTemplate>
							</ItemsControl.ItemTemplate>
						</ItemsControl>
						<layoutPlanning:FocusAndBlur DataContext="{Binding Layouts.SelectedProject,Source={StaticResource Locator}}" Top="{Binding PosY}"  Left="{Binding PosX}" >
							<layoutPlanning:FocusAndBlur.Bottom>
								<MultiBinding Converter="{StaticResource SubtractValues}">

                                    <Binding Path="Main.Layout.Height" Source="{StaticResource Locator }" FallbackValue="-1"/>
									<Binding Path="Project.ProjectAreaDef.Bottom"   FallbackValue="-1"/>
								</MultiBinding>
							</layoutPlanning:FocusAndBlur.Bottom>
							<layoutPlanning:FocusAndBlur.Right>
								<MultiBinding Converter="{StaticResource SubtractValues}">
                                    <Binding Path="Main.Layout.Width" Source="{StaticResource Locator }" FallbackValue="-1" />
									<Binding Path="Project.ProjectAreaDef.Right" FallbackValue="-1"/>
								</MultiBinding>
							</layoutPlanning:FocusAndBlur.Right>

						</layoutPlanning:FocusAndBlur>
						<ItemsControl ItemsSource="{Binding  Layouts.ProjectsInLayoutExceptSelected,Source={StaticResource Locator}}">
							<ItemsControl.ItemsPanel>
								<ItemsPanelTemplate>
									<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >

									</Grid>
								</ItemsPanelTemplate>
							</ItemsControl.ItemsPanel>
							<ItemsControl.ItemTemplate>
								<DataTemplate>

									<StackPanel    HorizontalAlignment="Left" VerticalAlignment="Top" Opacity="0.5" Background="{StaticResource StripedBrush}" Margin="{Binding Project.ProjectAreaDef, Converter={StaticResource RectangelToMargin}}" Width="{Binding  SizeX}" Height="{Binding SizeY}">
										<Viewbox Margin="0">
											<TextBlock Background="Azure" Opacity="0.5" Text="{Binding Project.ProjectAreaName }"></TextBlock>
										</Viewbox>
									</StackPanel>

								</DataTemplate>
							</ItemsControl.ItemTemplate>
						</ItemsControl>
						<!--<ed:BlockArrow Fill="{DynamicResource BaseBackground-Sketch}" ed:GeometryEffect.GeometryEffect="Sketch" Height="50" Orientation="Right" Stroke="{DynamicResource BaseBorder-Sketch}" StrokeThickness="2" Width="100"/>-->


						<ItemsControl ItemsSource="{Binding DeployedGraficalObjects}" >
							<ItemsControl.ItemsPanel>
								<ItemsPanelTemplate>

									<Grid Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"   AllowDrop="True" x:Name="Grid"  Margin="0" ClipToBounds="True" >
										<interactivity:Interaction.Behaviors>

											<eventToCommand:EventToCommand EventName="Drop" Command="{Binding NewDrop, Mode=OneWay}"
										EventArgsConverter="{StaticResource MouseButtonEventArgsToStationConverter}"
										
										PassEventArgsToCommand="True"/>
											<eventToCommand:EventToCommand EventName="MouseLeftButtonDown" Command="{Binding StartDraw, Mode=OneWay}"
										EventArgsConverter="{StaticResource MouseButtonEventArgsToPointConverter}" 
										PassEventArgsToCommand="True"/>


											<eventToCommand:EventToCommand EventName="MouseLeftButtonUp" Command="{Binding EndDraw, Mode=OneWay}"
										EventArgsConverter="{StaticResource MouseButtonEventArgsToPointConverter}"
										PassEventArgsToCommand="True"/>

											<eventToCommand:EventToCommand EventName="MouseEnter" Command="{Binding DragOffsetChanged, Mode=OneWay}" EventArgsConverter="{StaticResource DependencyPropertyChangedToBool}"  PassEventArgsToCommand="True"/>

											<eventToCommand:EventToCommand EventName="MouseMove" Command="{Binding DragOffsetChanged, Mode=OneWay}" EventArgsConverter="{StaticResource DependencyPropertyChangedToBool}"  PassEventArgsToCommand="True"/>


											<eventToCommand:EventToCommand EventName="MouseLeave"  Command="{Binding DragOffsetChanged, Mode=OneWay}" EventArgsConverter="{StaticResource DependencyPropertyChangedToBool}"  PassEventArgsToCommand="True" />

											<!--<i:EventTrigger EventName="LayoutUpdated">
									<command:EventToCommand Command="{Binding Refresh, Mode=OneWay}"/>
								</i:EventTrigger>-->
										</interactivity:Interaction.Behaviors>

									</Grid>
								</ItemsPanelTemplate>
							</ItemsControl.ItemsPanel>
							<ItemsControl.ItemTemplate>
								<DataTemplate>
									<UserControl RenderTransformOrigin="0.5,0.5" RenderTransform="{Binding Transform}" Width="{Binding SizeX}" Height="{Binding SizeY}" HorizontalAlignment="Left" VerticalAlignment="Top" >
										<interactivity:Interaction.Behaviors>

											<eventToCommand:EventToCommand EventName="MouseMove"  Command="{Binding RefreshBackground}" PassEventArgsToCommand="True" CommandParameter="{Binding}"/>

											<eventToCommand:EventToCommand EventName="MouseLeftButtonUp"  Command="{Binding Main.SelectGraficalObject, Source={StaticResource Locator}}" PassEventArgsToCommand="True" CommandParameter="{Binding}"/>
											<behaviours:DragBehavior/>
										</interactivity:Interaction.Behaviors>

										<Grid>
											<Grid Background="{Binding Background}" Visibility="{Binding ControlType, Converter={StaticResource TaskTypeToVisability}, ConverterParameter=Custom}" >
												<ContentControl   Content="{Binding CustomControl.Element}"  />
											</Grid>
											<Rectangle x:Name="Rectangle" Visibility="{Binding ControlType, Converter={StaticResource TaskTypeToVisability}, ConverterParameter=Square}" Width="{Binding SizeX}" Height="{Binding SizeY}" Fill="{Binding Background}" StrokeThickness="5" Stroke="{DynamicResource ButtonPressedBackground}" Opacity="70" >
												<interactivity:Interaction.Behaviors>
													<eventToCommand:EventToCommand EventName="IsVisibleChanged" Command="{Binding VisibleChanged}" PassEventArgsToCommand="True" CommandParameter="{Binding ElementName=Rectangle, Mode=OneWay}">

													</eventToCommand:EventToCommand>
												</interactivity:Interaction.Behaviors>
											</Rectangle>
											<ed:Arc x:Name="Arc"  Visibility="{Binding ControlType, Converter={StaticResource TaskTypeToVisability}, ConverterParameter=Circle}"  ArcThickness="100000" StartAngle="0" EndAngle="360"   StrokeThickness="10" Fill="{Binding Background}" Opacity="70">
												<interactivity:Interaction.Behaviors>
													<eventToCommand:EventToCommand EventName="IsVisibleChanged" Command="{Binding VisibleChanged}" PassEventArgsToCommand="True" CommandParameter="{Binding ElementName=Arc, Mode=OneWay}">

													</eventToCommand:EventToCommand>
												</interactivity:Interaction.Behaviors>
											</ed:Arc>

											<Viewbox Stretch="Fill" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
												<ed:Arc  Width="40" Height="40"
											Visibility="{Binding  Main.ShowSound, Source={StaticResource Locator}, Converter={StaticResource BoolToVisability}}" 
											x:Name="arc"  ArcThickness="1"   Fill="{Binding  SoundColor}" ArcThicknessUnit="Pixel" EndAngle="360"  Stretch="None" Stroke="{Binding SoundColor}" 
											StartAngle="0" StrokeThickness="3" RenderTransformOrigin="0.5,0.5" >
													<ed:Arc.Margin>
														<MultiBinding Converter="{StaticResource ValueToThickness}">
															<Binding Path="Loudness"/>
															<Binding RelativeSource="{RelativeSource Self}" />
														</MultiBinding>

													</ed:Arc.Margin>
													<ed:Arc.RenderTransform>
														<TransformGroup>
															<ScaleTransform/>
															<SkewTransform/>
															<RotateTransform/>
															<TranslateTransform/>
														</TransformGroup>
													</ed:Arc.RenderTransform>
													<ed:Arc.Resources>
														<Style TargetType="{x:Type ed:Arc}">
															<Style.Triggers>
																<DataTrigger Binding="{Binding Main.ShowSound, Source={StaticResource Locator}}" Value="True">
																	<DataTrigger.EnterActions>
																		<BeginStoryboard>
																			<Storyboard Name="SoundAnimation" RepeatBehavior="Forever" Target="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ed:Arc}}">
																				<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" >
																					<EasingDoubleKeyFrame KeyTime="0" Value="1" />
																					<EasingDoubleKeyFrame KeyTime="0:0:1" Value="2" />
																					<EasingDoubleKeyFrame KeyTime="0:0:2" Value="1" />
																				</DoubleAnimationUsingKeyFrames>
																				<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" >
																					<EasingDoubleKeyFrame KeyTime="0" Value="1" />
																					<EasingDoubleKeyFrame KeyTime="0:0:1" Value="2" />
																					<EasingDoubleKeyFrame KeyTime="0:0:2" Value="1" />
																				</DoubleAnimationUsingKeyFrames>
																			</Storyboard>
																		</BeginStoryboard>
																	</DataTrigger.EnterActions>
																	<DataTrigger.ExitActions>
																		<BeginStoryboard>
																			<Storyboard  FillBehavior="Stop">
																				<DoubleAnimation By="1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.Target="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ed:Arc}}" />
																				<DoubleAnimation By="1" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.Target="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ed:Arc}}" />
																			</Storyboard>
																		</BeginStoryboard>
																	</DataTrigger.ExitActions>
																</DataTrigger>
															</Style.Triggers>
														</Style>
													</ed:Arc.Resources>
												</ed:Arc>
											</Viewbox>

										</Grid>

									</UserControl>
								</DataTemplate>
							</ItemsControl.ItemTemplate>
						</ItemsControl>

						<ItemsControl   Visibility="{Binding ShowSpagetti, Converter={StaticResource BoolToVisability}}" ItemsSource="{Binding CurrentWorkOrder.Worktasks}">
							<ItemsControl.ItemsPanel>
								<ItemsPanelTemplate>
									<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
								</ItemsPanelTemplate>
							</ItemsControl.ItemsPanel>

							<ItemsControl.Resources>
								<DataTemplate  DataType="{x:Type model:WalkTask}">
									<shapes:Arrow   Stroke="CornflowerBlue" HeadHeight="4" HeadWidth="6"  StrokeThickness="{Binding Main.Zoom, Source={StaticResource Locator}, Converter={StaticResource ZoomToStrokeThickness}}"  
										X1="{Binding TaskFromStation.CenterPoint.X, Mode=OneWay}" Y1="{Binding TaskFromStation.CenterPoint.Y, Mode=OneWay}" 
										X2="{Binding TaskToStation.CenterPoint.X, Mode=OneWay}" Y2="{Binding TaskToStation.CenterPoint.Y, Mode=OneWay}" />
								</DataTemplate>
								<DataTemplate  DataType="{x:Type model:CarryTask}">
									<shapes:Arrow   Stroke="CornflowerBlue" HeadHeight="4" HeadWidth="6"  StrokeThickness="{Binding Main.Zoom, Source={StaticResource Locator}, Converter={StaticResource ZoomToStrokeThickness}}"  
										X1="{Binding TaskFromStation.CenterPoint.X, Mode=OneWay}" Y1="{Binding TaskFromStation.CenterPoint.Y, Mode=OneWay}" 
										X2="{Binding TaskToStation.CenterPoint.X, Mode=OneWay}" Y2="{Binding TaskToStation.CenterPoint.Y, Mode=OneWay}" />
								</DataTemplate>
								<DataTemplate  DataType="{x:Type model:WorkTask}">
									<shapes:Arrow  X1="{Binding TaskFromStation.CenterPoint.X, Mode=OneWay}" Y1="{Binding TaskFromStation.CenterPoint.Y, Mode=OneWay}" 
										X2="{Binding TaskFromStation.CenterPoint.X, Mode=OneWay}" Y2="{Binding TaskFromStation.CenterPoint.Y, Mode=OneWay}"  Stroke="CornflowerBlue" 
												   StrokeThickness="{Binding Main.Zoom, Source={StaticResource Locator}, Converter={StaticResource ZoomToStrokeThickness}}" 
												   HeadHeight="{Binding Main.Zoom, Source={StaticResource Locator}, Converter={StaticResource ZoomToStrokeThickness}}" 
												   HeadWidth="{Binding Main.Zoom, Source={StaticResource Locator}, Converter={StaticResource ZoomToStrokeThickness}}" />
								</DataTemplate>
							</ItemsControl.Resources>
						</ItemsControl>



					</Grid>

				</ZoomableCanvas>
			</Border>
			<StackPanel Orientation="Horizontal" Height="30" Margin="50,0" VerticalAlignment="Bottom" HorizontalAlignment="Left">
				<TextBlock HorizontalAlignment="Left" Margin="3,2"  Text="{Binding Zoom, Converter={StaticResource ZoomToLable} , StringFormat=0.#}"/>
				<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="2,2"  Text="(m)"/>
				<Viewbox Width="{Binding Zoom, Converter={StaticResource ZoomToWidth} }"   Stretch="Fill" Height="20">
					<Canvas Width="100" Height="20">
						<Path Stroke="Black">
							<Path.Data>
								<LineGeometry EndPoint="100,10" StartPoint="0,10"/>
							</Path.Data>
						</Path>
						<Path Stroke="Black">
							<Path.Data>
								<LineGeometry EndPoint="0,0" StartPoint="0,20"/>
							</Path.Data>
						</Path>
						<Path Stroke="Black">
							<Path.Data>
								<LineGeometry EndPoint="100,0" StartPoint="100,20"/>
							</Path.Data>
						</Path>
					</Canvas>
				</Viewbox>
			</StackPanel>
		</Grid>

	</Border>

</UserControl>